#include "process.h"



const char *str = "|/-\\";



void process_v1()
{
    //version 1
    int rate = 0;
    char bar[SIZE] = {0};
    int num = strlen(str);

    while(rate <= MAX_RATE)
    {
     printf("[%-100s][%d%%][%c]\r", bar, rate, str[rate%num]);
      fflush(stdout);
      usleep(STIME);
      bar[rate++] = STYLE;
    }
    printf("\n");
 
}


//不能一次将进度条打印完毕，否则无法平滑的和场景结合
//该函数应该根据rate自动的打印一次
void process_v2(int rate)
{
    //version 2
   

    static char bar[SIZE] = {0};
    
    int num = strlen(str);

    if(rate <= MAX_RATE && rate >= 0)
    {
      printf("[%-100s][%d%%][%c]\r", bar, rate, str[rate%num]);
      fflush(stdout);
  
      bar[rate] = STYLE;
    }
    if(rate == MAX_RATE)
    {
      memset(bar, '\0', sizeof(bar));
    }

}





//完善
void process_v3(double rate)

{
    //version 2
   

    static char bar[SIZE] = {0};
    static int cnt = 0; 
    int num = strlen(str);

    if(rate <= MAX_RATE && rate >= 0)
    {
      cnt++;
      cnt %= num;
      printf("[%-100s][%.1f%%][%c]\r", bar, rate, str[cnt]);
      fflush(stdout);
  
      if(rate < MAX_RATE)
      {
        bar[(int)rate] = STYLE_BODY;// '='
        bar[(int)rate + 1] = STYLE_HEADER;//'>'
      
      }
      else
      {
        bar[(int)rate] = STYLE_BODY;
      }
    }
    //if(rate == MAX_RATE)
    //{
     // memset(bar, '\0', sizeof(bar));
    //}
}




